#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "menu.h"
#include "meilleure.h"

int fenetre_menu();
int fenetre_score();
int fenetre_aide();

void menu(SDL_Surface *screen, int *continuer)
{
SDL_Surface *image;
SDL_Event event;
SDL_Rect background,src;

image=SDL_LoadBMP("img/image.bmp");
(*continuer)=1;
while(*continuer!=0 && *continuer!=4 )
    {if(*continuer==1)
    *continuer=fenetre_menu();
    else if(*continuer==2)
    {*continuer=fenetre_aide();
    }
    else if(*continuer==3)
    { screen=SDL_SetVideoMode(887,612,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
     if(screen==NULL)
    {printf ("erreur");
    }
    
    if(SDL_Init(SDL_INIT_VIDEO)!=0)
    {printf("erreur");
     
    }
    if(image==NULL)
    {printf("erreur");
     
    }
     SDL_WM_SetCaption("Pac-Man",NULL);

     src.x=0;
     src.y=0;
     src.w=image->w; 
     src.h=image->h;
     SDL_BlitSurface(image,NULL,screen,&src);
    SDL_Flip(screen);
    *continuer=4;
	}
	else if(*continuer==5)
	       {*continuer=fenetre_score();
	       }
    }
 // if(continuer==2)
   //  {
     //   SDL_Quit();
       // return 0;
     //}
//else
//{

/*if(SDL_Init(SDL_INIT_VIDEO)!=0)
{printf("erreur");
return (1);
}

screen=SDL_SetVideoMode(500,500,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen==NULL)
{printf ("erreur");
return 1;
}
SDL_WM_SetCaption("Menu PacMan",NULL);
image=SDL_LoadBMP("img/image.bmp");
if(image==NULL)
{printf("erreur");
return(1);
}*/

 /*background.x=0;
 background.y= 0;
 background.h=image->h;
 background.w=image->w;
 SDL_BlitSurface(image,NULL,screen,&background);*/
 //SDL_Flip(screen);


}



//Menu Principal
int fenetre_menu()
{
SDL_Surface *screen2;
SDL_Surface *image2;
SDL_Surface *image;

SDL_Rect position_menu,jouer,aide,score,quitter;
int makrem=1;
int retour=1,numero=0;
image=SDL_LoadBMP("img/image.bmp");

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{printf("erreur");
return (1);
}

screen2=SDL_SetVideoMode(887,612,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen2==NULL)
{printf ("erreur");
return 1;
}

image2=SDL_LoadBMP("img/menu.bmp");

if(image2==NULL)
{printf("erreur");
return(1);
}

SDL_WM_SetCaption("Menu PacMan",NULL);

position_menu.x=0;
position_menu.y=0;
position_menu.h=image2->h;
position_menu.w=image2->w;

jouer.x=400;
jouer.y=294;
jouer.h= 30;
jouer.w= 115;

aide.x=410;
aide.y=452;
aide.h=30;
aide.w=90;

score.x=404;
score.y=371;
score.h=30;
score.w=110;

quitter.x=380 ;
quitter.y=525 ;
quitter.h=40;
quitter.w=150;

SDL_BlitSurface(image2,NULL,screen2,&position_menu);
SDL_Flip(screen2);

SDL_Event event;
while(makrem)
    {
    SDL_WaitEvent(&event);
       switch(event.type)
        {
        case SDL_MOUSEBUTTONDOWN :
        if(event.button.button ==  SDL_BUTTON_LEFT && event.button.x>jouer.x && event.button.x<((jouer.x)+jouer.w)
           && event.button.y>jouer.y && event.button.y<((jouer.y)+jouer.h))
            {
             makrem=0;
             retour=3;
            }
        else if(event.button.button == SDL_BUTTON_LEFT && event.button.x>quitter.x && event.button.x<((quitter.x)+quitter.w)
                && event.button.y>quitter.y && event.button.y<((quitter.y)+quitter.h))
                    {
                    makrem=0;
                    retour=0;
                    }
        else if(event.button.button == SDL_BUTTON_LEFT && event.button.x>aide.x && event.button.x<((aide.x)+aide.w)
                && event.button.y>aide.y && event.button.y<((aide.y)+aide.h))
                    {
                    numero=1;
                    makrem=0;
                    retour=2;
                    }
        else if(event.button.button == SDL_BUTTON_LEFT && event.button.x>score.x && event.button.x<((score.x)+score.w)
                && event.button.y>score.y && event.button.y<((score.y)+score.h))
                    {
                    numero=1;
                    makrem=0;
                    retour=5;
                    }
       }
    }

    
    
return (retour);
}

//MENU SCORE
int fenetre_score()
{SDL_Surface *screen4;
SDL_Surface *image4;
SDL_Rect position_score,pos_retour;
int retour,makrem=1,scores=10;
int x1,x2,x3,y;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{printf("erreur");
return (1);
}

screen4=SDL_SetVideoMode(887,612,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen4==NULL)
{printf ("erreur");
return 1;
}

image4=SDL_LoadBMP("img/meilleure.bmp");

if(image4==NULL)
{printf("erreur");
return(1);
}

SDL_WM_SetCaption("Menu Score",NULL);
	
position_score.x=0;
position_score.y=0;
position_score.h=image4->h;
position_score.w=image4->w;

SDL_BlitSurface(image4,NULL,screen4,&position_score);
SDL_Flip(screen4);

x1=460;
x2=440;
x3=420;
y=350;

meilleure_score(scores,x1,x2,x3,y,screen4);
SDL_Flip(screen4);

pos_retour.x=360;
pos_retour.y=542;
pos_retour.h=34;
pos_retour.w=140;

SDL_Event event;
while(makrem)
    {
    SDL_WaitEvent(&event);
       switch(event.type)
        {
        	case SDL_MOUSEBUTTONDOWN :
        if(event.button.button ==  SDL_BUTTON_LEFT && event.button.x>pos_retour.x && event.button.x<((pos_retour.x)+pos_retour.w)
           && event.button.y>pos_retour.y && event.button.y<((pos_retour.y)+pos_retour.h))
            {
             makrem=0;
             retour=1;
            }

        }
    }
return (retour);


}



//MENU AIDE
int fenetre_aide()
{SDL_Surface *screen3;
SDL_Surface *image3;
SDL_Rect position_aide,pos_retour;
int retour,makrem=1;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{printf("erreur");
return (1);
}

screen3=SDL_SetVideoMode(887,612,16,SDL_HWSURFACE | SDL_DOUBLEBUF);
if(screen3==NULL)
{printf ("erreur");
return 1;
}

image3=SDL_LoadBMP("img/aide.bmp");

if(image3==NULL)
{printf("erreur");
return(1);
}

SDL_WM_SetCaption("Menu Aide",NULL);
	
position_aide.x=0;
position_aide.y=0;
position_aide.h=image3->h;
position_aide.w=image3->w;
	
SDL_BlitSurface(image3,NULL,screen3,&position_aide);
SDL_Flip(screen3);

pos_retour.x=393;
pos_retour.y=538;
pos_retour.h=24;
pos_retour.w=107;	
	
SDL_Event event;
while(makrem)
    {
    SDL_WaitEvent(&event);
       switch(event.type)
        {
        	case SDL_MOUSEBUTTONDOWN :
        if(event.button.button ==  SDL_BUTTON_LEFT && event.button.x>pos_retour.x && event.button.x<((pos_retour.x)+pos_retour.w)
           && event.button.y>pos_retour.y && event.button.y<((pos_retour.y)+pos_retour.h))
            {
             makrem=0;
             retour=1;
            }

        }
    }
return (retour);
}

